<?php

namespace App\Service;

use App\Models\Adv;
use App\Models\Article;
use App\Models\Banner;
use App\Models\Category;
use App\Models\Configs;
use App\Models\Nav;
use App\Models\Tags;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Request;

class SiteService
{
    // protected $locale = 'zh-CN'; // 默认语言 / en

    public function __construct()
    {
        // $this->locale = Request::cookie('locale');
    }

    public function getNavs()
    {
        $key = 'cache:navs';
        // if (Cache::has($key)) {
        //     return Cache::get($key);
        // }
        $navs = Nav::where('hide', 0)->latest('orderby')->get();
        Cache::put($key, $navs, 86400);
        return $navs;
    }
    public function homeNavs()
    {
        $list = $this->getNavs();
        $navs = $list->filter(function ($value, $key) {
            return $value['isHome'] == 1;
        });
        return $navs;
        // $key = 'cache:home:navs';
        // // if (Cache::has($key)) {
        // //     return Cache::get($key);
        // // }
        // $navs = Nav::where('isHome', 1)->where('hide', 0)->latest('orderby')->get();
        // Cache::put($key, $navs, 86400);
        // return $navs;
    }

    /**
     * 获取网站配置
     *
     * @return mixed
     */
    public function getConfigs()
    {
        $key = 'cache:configs';
        if (Cache::has($key)) {
            return Cache::get($key);
        }
        $configs = Configs::pluck('value', 'key');
        Cache::put($key, $configs, 86400);
        return $configs;
    }

    public function getAdvs()
    {
        $key = 'cache:adv';
        if (Cache::has($key)) {
            return Cache::get($key);
        }
        $banners = Adv::pluck('image', 'type');
        Cache::put($key, $banners, 86400);
        return $banners;
    }


    /**
     * 获取网站首页轮播图
     *
     * @return mixed
     */
    public function getBanners()
    {
        $key = 'cache:banner';
        if (Cache::has($key)) {
            return Cache::get($key);
        }
        $banners = Banner::where('hide', 0)->latest('orderby')->get();
        Cache::put($key, $banners, 86400);
        return $banners;
    }


    /**
     * 获取网站分类
     *
     * @return mixed
     */
    public function getCategorys()
    {
        $key = 'cache:category';
        if (Cache::has($key)) {
            return Cache::get($key);
        }
        $categorys = Category::get();
        Cache::put($key, $categorys, 86400);
        return $categorys;
    }

    public function categoryBy($type)
    {
        $result = [];
        if (empty($type)) return $result;

        $array = $this->getCategorys();
        foreach ($array as $item) {
            if ($item['type'] == $type) {
                $result[] = $item;
            }
        }
        return $result;
    }

    public function getTags()
    {
        $key = 'cache:tags';
        if (Cache::has($key)) {
            return Cache::get($key);
        }
        $list = Tags::get();
        Cache::put($key, $list, 3600);
        return $list;
    }

    /**
     * 首页推荐文章
     *
     * @param int $type
     * @return mixed
     */
    public function recommend($type, $limit = 10)
    {
        return Article::with('category:id,title')
            ->when($type, function ($query) use ($type) {
                $query->where(compact('type'));
            })
            ->where('isrecommend', 1)
            ->latest('orderby')
            ->limit($limit)
            ->get([
                'id',
                'type',
                'category_id',
                'title',
                'image',
                'desc',
                'created_at',
            ]);
    }

    public function moduleArticleList($type, $limit = 8)
    {
        $articles = Article::select('id', 'type', 'category_id', 'title', 'image', 'desc')
            ->when($type, function ($query) use ($type) {
                $query->where(compact('type'));
            })
            ->latest('orderby')
            ->limit($limit)
            ->get();

        return $articles;
    }

    public function homeArticleList($cid, $limit = 8)
    {
        $articles = Article::select('id', 'type', 'category_id', 'title', 'image', 'desc')
            ->when($cid, function ($query) use ($cid) {
                $query->where('category_id', $cid);
            })
            ->latest('orderby')
            ->limit($limit)
            ->get();

        return $articles;
    }



    /**
     * 文章详情页
     *
     * @param int $type
     * @param int $id
     * @return mixed
     */
    public function detail($id, $type = 0)
    {
        $info = Article::where('id', $id)->when($type, function ($query) use ($type) {
            $query->where(compact('type'));
        })->first();
        if ($info) {
            return $info->load('nav:id,name', 'category:id,title');
        }
        return $info;
    }



    public function list($type, $categoryId = '', $tag = '')
    {
        return Article::with('category:id,title')
            ->select([
                'id',
                'type',
                'category_id',
                'title',
                'image',
                'desc',
                'created_at',
            ])
            ->where(compact('type'))
            ->when($categoryId, function ($q, $categoryId) {
                $q->where('category_id', $categoryId);
            })
            ->when($tag, function ($q, $tag) {
                $q->where('tags', 'like', '%' . $tag . '%');
            })
            ->latest('orderby')
            ->paginate(12);
    }



    public function homeTalentList($type)
    {
        $categorys = $this->categoryBy($type);
        $ids = array_column($categorys, 'id');

        $articles = Article::where('type', $type)
            ->whereIn('category_id', $ids)
            ->latest('orderby')
            ->get(['id', 'type', 'category_id', 'title', 'image']);

        $result = [];
        if ($articles->count()) {
            foreach ($articles as $article) {
                $result[$article->category_id][] = $article;
            }
        }

        foreach ($categorys as $ct) {
            $ct->items = !empty($result[$ct->id]) ? $result[$ct->id] : [];
        }
        return $categorys;
    }
}
